import dayjs from "dayjs"

export default function(app) {
  app.directive("format-time", {
    created(_, bindings) {
      bindings.formatString = "YYYY-MM-DD HH:mm:ss"
      if (bindings.value) {
        bindings.formatString = bindings.value
      }
    },
    mounted(el, bindings) {
      const text = el.innerHTML
      let timestamp = parseInt(text)
      if (text.length === 10) {
        timestamp = timestamp * 1000
      }
      el.innerHTML = dayjs(timestamp).format(bindings.formatString)
    }
  })
}